package com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Student implements Comparable<Student> {

    public String name;
    public int age;

    public Student(String name, int age) {
       this.name = name;
       this.age = age;
    }

    @Override
    public String toString() {
        return name + ":" + age;
    }

    @Override
    public int compareTo(Student o) {
        return Comparators.NAME.compare(this, o);
    }

    private static Comparator<Student> NAME = new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.name.compareTo(o2.name);
        }
    };
    
    public static class Comparators {

    	private static Comparator<Student> NAME = new Comparator<Student>() {
            
            public int compare(Student o1, Student o2) {
                return o1.name.compareTo(o2.name);
            }
        };
        private static Comparator<Student> AGE = new Comparator<Student>() {
            
            public int compare(Student o1, Student o2) {
                return o1.age - o2.age;
            }
        };
        private static Comparator<Student> NAMEANDAGE = new Comparator<Student>() {
            
            public int compare(Student o1, Student o2) {
                int i = o1.name.compareTo(o2.name);
                if (i == 0) {
                    i = o1.age - o2.age;
                }
                return i;
            }
        };
    }
    
    public static void main(String[] args){
    	List<Student> studentList = new ArrayList<>();
    	studentList.add(new Student("zhang", 11));
    	studentList.add(new Student("dong", 8));
    	studentList.add(new Student("huahua", 90));
    	Collections.sort(studentList, Student.Comparators.AGE);
    	
    	for(Student temp : studentList){
    		System.out.println(temp.toString());
    	}
    }
}